﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FortuneHeaven.BLL.Game;
using FortuneHeaven.BLL.NetBar;
using Jzy.Common;
using FortuneHeaven.Entity.NetBar;
using FortuneHeaven.Entity.Game;
using FortuneHeavenCommon;
using System.Xml.Linq;
using System.IO;

namespace FortuneHeaven.Admin.Pages.GameManager
{
    public partial class Config : PageEdit
    {

        private GameKindItemBO GameKind
        {
            get
            {
                return GameKindItemBO.Instance<GameKindItemBO>();
            }
        }

        private UserExBO UserEx
        {
            get
            {
                return UserExBO.Instance<UserExBO>();
            }
        }

        private int? _netBarID;
        private int NetBarID
        {
            get
            {
                if (_netBarID == null)
                {
                    UserExBO user = new UserExBO();
                    UserExInfo info = user.GetInfo(AppSession.UserID);
                    if (info != null)
                    {
                        _netBarID = info.NetBarID;
                    }
                    else
                    {
                        _netBarID = 0;
                    }
                }
                return _netBarID.Value;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            //sharkcfg  鲨鱼 6305_shark.xml
            //fivestarcfg  五星 1021_fivestar.xml
            //3danimal  森林 6131_3danimal.xml
            //Fishcfg   捕鱼 6100_Fish.xml

            //游戏名称
            //五星宏辉   FourEnsign.exe
            //金鲨银鲨   shark.exe
            //森林舞会   3DSLWG_CLIENT.exe
            //捕鱼达人   Fish.exe


            if (!IsPostBack)
            {
                List<GameKindItemInfo> list = GameKind.GetListByNetBarID(NetBarID);//
                IEnumerable<GameKindItemInfo> tmp = list.Where(T => T.ProcessName.Trim().ToLower() == "shark.exe");

                if (tmp.Count() == 0)
                {
                    Alert("数据库缺少该网吧的鲨鱼kindID");
                }
                else
                {
                    string xmlPath = WebConfig.GamePath + "sharkcfg" + "\\" + tmp.First().KindID + "_shark.xml";

                    if (File.Exists(xmlPath))
                    {

                        XElement ele = XElement.Load(xmlPath);
                        XElement child = ele.Elements().Where(T => T.Attribute("AttName").Value.Trim() == "Node2").First();
                        txtMode.Text = child.Element("Node11").Attribute("mode").Value;
                        txtLos.Text = child.Element("Node12").Attribute("los").Value;
                        txtWin.Text = child.Element("Node13").Attribute("win").Value;
                        txtShark.Text = child.Element("Node14").Attribute("shark").Value;
                        txtBoun.Text = child.Element("Node15").Attribute("boun").Value;
                    }
                    else
                    {
                        Alert("“" + xmlPath + "”不存在");
                    }                    
                }                
            }
        }

        
        protected void btnSave_Click(object sender, EventArgs e)
        {
            List<GameKindItemInfo> list = GameKind.GetListByNetBarID(NetBarID);//
            IEnumerable<GameKindItemInfo> tmp = list.Where(T => T.ProcessName.Trim().ToLower() == "shark.exe");

            if (tmp.Count() == 0)
            {
                Alert("数据库缺少该网吧的鲨鱼kindID");
            }
            else
            {
                string xmlPath = WebConfig.GamePath + "sharkcfg" + "\\" + tmp.First().KindID + "_shark.xml";
                if (File.Exists(xmlPath))
                {
                    XElement ele = XElement.Load(xmlPath);
                    XElement child = ele.Elements().Where(T => T.Attribute("AttName").Value.Trim() == "Node2").First();
                    child.Element("Node11").Attribute("mode").Value = txtMode.Text;
                    child.Element("Node12").Attribute("los").Value = txtLos.Text;
                    child.Element("Node13").Attribute("win").Value = txtWin.Text;
                    child.Element("Node14").Attribute("shark").Value = txtShark.Text;
                    child.Element("Node15").Attribute("boun").Value = txtBoun.Text;

                    ele.Save(xmlPath);

                    Alert("保存成功");
                }
                else
                {
                    Alert("“" + xmlPath + "”不存在");
                }

            }
        }
    }
}